﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Data.Entity;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using EKAABSInformationManagement.Service;
using EKAABSInformationManagement.Models;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Controllers
{
    public class CardController : Controller
    {
        public CardService myCard { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (System.Web.HttpContext.Current.User.IsInRole("admin") != true)
            {
                HttpException ex = new HttpException(401, "");
                requestContext.HttpContext.AddError(ex);
            }
            if (myCard == null) { myCard = new CardService(); }
            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            List<cardInBell> Card = myCard.getAllCard();
            return View(Card);
        }

        [HttpPost]
        public ActionResult submit(cardInBell a)
        {
            try
            {
                bool isExists = default(bool);
                myCard.insert(a, out isExists);

                if (isExists != false)
                {
                    return Json(new { status = 1 });
                }
                else
                {
                    return Json(new { status = 2 });
                }
            }
            catch { return Json(new { status = 4 }); }
        }
        [HttpGet]
        public ActionResult getAllUsers()
        {
            try
            {
                var json = new JsonResult();
                employeeService service = new employeeService();
                List<employee> employees = service.getAllEmployeesInBell();
                StringBuilder result = new StringBuilder();
                result.Append("{\"employees\":[");

                foreach (employee e in employees)
                {
                    result.Append("{");
                    result.AppendFormat("\"alias\":\"{0}\"", e.alias);
                    result.Append("},");
                }

                result.Remove(result.Length - 1, 1);
                result.Append("]}");
                return Content(result.ToString());
            }
            catch { return Content("error"); }
        }


        [HttpPost]
        public ActionResult update(cardInBell c)
        {
            try
            {
                bool isExists = default(bool);
                myCard.update(c,out isExists);
                if (isExists != false)
                {
                    return Json(new { status = 1 });
                }
                else
                {
                    return Json(new { status = 2 });
                }
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult delete(string ids)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                myCard.delete(strs);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
        [HttpPost]
        public ActionResult batchUpdate(string ids, cardInBell c)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                myCard.batchUpdate(strs, c);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
    }
}
